package socketHandler;

import java.util.ArrayList;
/**
 * 
 * @author Peter Steiner & Fredrik Wallenius
 *
 */
public class Protocoll {
	
	public final static String MESG = "MESG ";
	public final static String USRS = "USRS";
	final static String SYNC = "SYNC ";
	final static String ACKD = "ACKD ";
	public final static String PING = "PING";
	final static int nrOfStrings= 5;
	final static int MESGTYPE = 0;
	final static int BATCH = 1;
	final static int TIMESTAMP = 2;
	final static int NAME = 3;
	final static int MESSAGE = 4;
	
	static public String processInput(String str, clientData data){
		ArrayList<String> stringList = new ArrayList<String>();
		stringList = StringHandler.MessageType(str);
		
		if(stringList.get(MESGTYPE).equalsIgnoreCase(MESG)){
			return "[" + stringList.get(TIMESTAMP) + "]" + "[" + stringList.get(NAME) + "]: "
				+ stringList.get(MESSAGE);
		}
		if(stringList.get(MESGTYPE).equalsIgnoreCase(USRS)){
			
		}
		if(stringList.get(MESGTYPE).equalsIgnoreCase(SYNC)){
			data.setServerBatch(stringList.get(BATCH));
			System.out.println("[Client]: recieved SYNC " + data.getServerBatch());
		}
		if(stringList.get(MESGTYPE).equalsIgnoreCase(ACKD)){
			System.out.println("recieved ACKD " + stringList.get(BATCH));
			
			//data.setServerBatch(stringList.get(BATCH));
		}
		return str;
	}
	static public String processOutput(String str, clientData data ){
		StringBuilder sb = new StringBuilder();
		sb.append("MESG ");
		sb.append("<" + data.getBatchNumber() + ">");
		sb.append("<" + str + ">");
		return sb.toString();
	}
	static public String processSYNC(clientData data){
		StringBuilder sb = new StringBuilder();
		sb.append("SYNC ");
		sb.append("<" + data.getBatchNumber() + ">");
		System.out.println("[Client]: sending " + sb.toString());
		return sb.toString();
	}
	static public String processACKD(clientData data){
		StringBuilder sb = new StringBuilder();
		sb.append("ACKD ");
		sb.append("<" + data.getServerBatch() + ">");
		System.out.println("[Client]: sending " + sb.toString());
		return sb.toString();
	}
	static public String processNAME(String str){
		StringBuilder sb = new StringBuilder();
		sb.append("NAME ");
		sb.append("<" + str + ">");
		return sb.toString();
	}
	
	static public String extractCommand(String str){
		String cmd;
		try{
			cmd = str.substring(0, 4);
		} catch (Exception e){
			return null;
		}
		return cmd;
	}
	


}
